package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ForgotPwdActivity extends AppCompatActivity {

    private ImageView forgot_pwd_back;

    private EditText forgot_pwd_Username;
    private EditText forgot_pwd_Carid;
    private EditText forgot_pwd_newpwd;

    private Button forgot_pwd_baocun;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_pwd);
        initid();
        forgot_pwd_baocun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                baocun();
            }
        });
        forgot_pwd_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initid(){
        forgot_pwd_back = (ImageView) findViewById(R.id.forgot_pwd_back);

        forgot_pwd_Username = (EditText)findViewById(R.id.forgot_pwd_Username);
        forgot_pwd_Carid = (EditText)findViewById(R.id.forgot_pwd_Carid);
        forgot_pwd_newpwd = (EditText)findViewById(R.id.forgot_pwd_newpwd);

        forgot_pwd_baocun = (Button) findViewById(R.id.forgot_pwd_baocun);

    }


    private void baocun(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("userName",forgot_pwd_Username.getText().toString());
                    jsonObject.put("idcard",forgot_pwd_Carid.getText().toString());
                    jsonObject.put("newPwd",forgot_pwd_newpwd.getText().toString());
                    MediaType mediaType = MediaType.parse("application/json");
                    RequestBody requestBody = RequestBody.create(mediaType,""+jsonObject.toString());
                    Request request1 = new Request.Builder()
                            .url(Sto.NetIP+"/api/User/ForgotPassword?idcard="+forgot_pwd_Carid.getText().toString()+"&newPwd="+forgot_pwd_newpwd.getText().toString()+"&userName="+forgot_pwd_Username.getText().toString())
                            .put(requestBody)
                            .build();
                    Response response = client.newCall(request1).execute();

                    String results = response.body().string();
                    JSONObject jsonObject1 = new JSONObject(results);
                    String message = jsonObject1.getString("message");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (message.equals("succeed")){
                                Toast.makeText(ForgotPwdActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                                finish();
                            }else {
                                Toast.makeText(ForgotPwdActivity.this,message,Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}